from . import models

from django.conf import settings
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from user.utils.permission import AdminPermission

import json
import string
class AdView(APIView):
    def get(self, request, *args, **kwargs):
        try:
            ret = {}
            listData = []
            name = request.GET.get('name')
            ADobj = models.AdData.objects.get(name = name)
            img = list(eval(ADobj.img))
            url = list(eval(ADobj.url))
            config = list(eval(ADobj.config))
            for index in range(len(img)):
                objData = {}
                objData['src'] = img[index]
                objData['url'] = url[index]
                if len(img) == 1:
                    json.dumps(objData)
                    return JsonResponse(objData)
                listData.append(objData)

            cof = request._request.GET.get('cof')
            if cof:
                ret = {'data':listData,'config':config}
                json.dumps(ret)
                return JsonResponse(ret)
            json.dumps(listData)
            ret['data'] = listData
        except:
            pass
        return JsonResponse(listData,safe=False)
            